package com.ilink.ilinkuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ilink.ilinkcommon.utils.SnowflakeIdGenerator;
import com.ilink.ilinkcommon.utils.UserContext;
import com.ilink.ilinkuser.DTO.UserApiKeyUpdateAndDeleteRequest;
import com.ilink.ilinkuser.DTO.UserApiKeysRequest;
import com.ilink.ilinkuser.domain.VO.UserApiKeyResponse;
import com.ilink.ilinkuser.entity.Model;
import com.ilink.ilinkuser.entity.ModelApiKey;
import com.ilink.ilinkuser.entity.UserApiKeys;
import com.ilink.ilinkuser.mapper.ModelApiKeyMapper;
import com.ilink.ilinkuser.mapper.ModelMapper;
import com.ilink.ilinkuser.mapper.UserApiKeysMapper;
import com.ilink.ilinkuser.service.UserApiKeysService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class UserApiKeysServiceImpl  implements UserApiKeysService {
    @Autowired
    private UserApiKeysMapper userApiKeysMapper;
    @Autowired
    private ModelApiKeyMapper modelApiKeyMapper;
    @Autowired
    private ModelMapper modelMapper;
    @Autowired
    private ObjectMapper objectMapper;

    private final SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(2, 2);
    @Override
    public List<Map<String,Object>> getUserApiKeysByUserIdAndModelId(String userId, Long modelId) {
        LambdaQueryWrapper<UserApiKeys> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserApiKeys::getUserId, userId)
                .eq(UserApiKeys::getModelId, modelId);

        List<UserApiKeys> userApiKeys=userApiKeysMapper.selectList(queryWrapper);
        List<Map<String, Object>> responseList = new ArrayList<>();

        for (UserApiKeys userApiKey : userApiKeys) {
            Map<String, Object> responseMap = new LinkedHashMap<>();
            responseMap.put("customPrimary", userApiKey.getCustomPrimary());
            responseMap.put("dataName", userApiKey.getDataName());

            // 解析 fieldsData，并将其内容加入到 responseMap
            String fieldsData = userApiKey.getFieldsData();
            try {
                Map<String, Object> fieldsMap = objectMapper.readValue(fieldsData, new TypeReference<Map<String, Object>>() {});
                responseMap.putAll(fieldsMap); // 将 fieldsData 中的所有字段添加到 responseMap
            } catch (Exception e) {
                e.printStackTrace();
                // 处理异常情况，可能设置一个默认值
            }

            responseList.add(responseMap);
        }
        return responseList;


    }
    @Override
    public boolean saveUserApiKey(UserApiKeysRequest userApiKey){
        String userId = UserContext.getUserId();
        Long customPrimary=idGenerator.nextId();

        UserApiKeys userApiKeys = new UserApiKeys();
        userApiKeys.setCustomPrimary(customPrimary);
        userApiKeys.setUserId(userId);
        userApiKeys.setModelId(userApiKey.getModelId());
        userApiKeys.setFieldsData(userApiKey.getFieldsData());
        userApiKeys.setDataName(userApiKey.getDataName());

        return userApiKeysMapper.insert(userApiKeys)>0;
    }
    @Override
    public boolean updateUserApiKey(UserApiKeyUpdateAndDeleteRequest userApiKey){
        String userId = UserContext.getUserId();
        UserApiKeys userApiKeys = new UserApiKeys();
        userApiKeys.setCustomPrimary(userApiKey.getCustomPrimary());
        userApiKeys.setUserId(userId);
        userApiKeys.setModelId(userApiKey.getModelId());
        userApiKeys.setFieldsData(userApiKey.getFieldsData());
        userApiKeys.setDataName(userApiKey.getDataName());

        return userApiKeysMapper.updateById(userApiKeys)>0;
    }
    @Override
    public boolean deleteUserApiKey(UserApiKeyUpdateAndDeleteRequest userApiKey){
        String userId = UserContext.getUserId();
        UserApiKeys userApiKeys = new UserApiKeys();
        userApiKeys.setCustomPrimary(userApiKey.getCustomPrimary());
        userApiKeys.setUserId(userId);
        userApiKeys.setModelId(userApiKey.getModelId());
        userApiKeys.setFieldsData(userApiKey.getFieldsData());
        userApiKeys.setDataName(userApiKey.getDataName());

        return userApiKeysMapper.deleteById(userApiKey)>0;
    }

    public ModelApiKey getFieldsByModelId(Long  modelId) {

        return modelApiKeyMapper.selectById(modelId);
    }
    public List<Model> findAllModels(){
        return modelMapper.selectList(null);
    }
    @Override
    public List<Model> getModelsByName(String modelName){
        LambdaQueryWrapper<Model> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Model::getModelName, modelName );
        return modelMapper.selectList(queryWrapper);
    }

}
